package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import jdk.internal.org.objectweb.asm.Handle;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    StringRedisTemplate redisTemplate;


    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        //1. 判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        //2. 未登录 直接将 4种行为 均返回false
        Map result=new HashMap<>();
        if (user==null){
            result.put("isfollow",false);
            result.put("islike",false);
            result.put("isunlike",false);
            result.put("iscollection",false);
            return ResponseResult.okResult(result);
        }
        //3. 如果登录
        //4. 查询行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(Long.valueOf(dto.getArticleId())));
        //5. 根据行为实体 文章ID查询 是否点赞
        ApLikesBehavior likesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        if(likesBehavior==null){
            result.put("islike",false);
        }else {
            result.put("islike",true);
        }
        //6. 根据行为实体 文章ID查询 是否不喜欢
        ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        if(unlikesBehavior==null){
            result.put("isunlike",false);
        }else {
            result.put("isunlike",true);
        }
        //7. 根据行为实体 文章ID查询 是否收藏
        ApCollection collection = mongoTemplate.findOne(query, ApCollection.class);
        if(collection==null){
            result.put("iscollection",false);
        }else {
            result.put("iscollection",true);
        }
        //8. 更具登录用户id 去redis中查询是否关注该作者
       /* Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + user.getId(), dto.getAuthorApUserId());
        if(score!=null){
            result.put("isfollow",true);
        }else {
            result.put("isfollow",false);
        }*/
        result.put("isfollow",false);
        //9. 封装结果 返回
        return ResponseResult.okResult(result);
    }
}
